import os
import pathlib
from dotenv import load_dotenv

# 获取app目录的绝对路径
app_dir = pathlib.Path(__file__).parent.absolute()

# 根据环境变量加载对应的 .env 文件
env = os.getenv('ENV', 'dev')  # 默认加载开发环境
env_file = app_dir / f".env.{env}"
load_dotenv(dotenv_path=env_file)

class Config:
    # 数据库配置  后端配置
    API_URL = os.getenv('API_URL','')
    SECRET_KEY = os.getenv('SECRET_KEY', '')

    # 构建正确的数据库连接字符串
    DATABASE_HOST = os.getenv('DATABASE_HOST', '')
    DATABASE_PORT = os.getenv('DATABASE_PORT', '')
    DATABASE_USER = os.getenv('DATABASE_USER', '')
    DATABASE_PASSWORD = os.getenv('DATABASE_PASSWORD', '')
    DATABASE_DATABASE = os.getenv('DATABASE_DATABASE', '')
    
    # 优先使用完整的 DATABASE_URL，如果不存在则构建
    DATABASE_URI = os.getenv('DATABASE_URL', '') or f"mysql+mysqlconnector://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_DATABASE}"
    
    # SSO配置
    SSO_SERVER_URL = os.getenv('SSO_URL', '')
    SSO_SERVER_INNER_URL = os.getenv('SSO_INNER_URL', '')
    SSO_CLIENT_ID = os.getenv('SSO_CLIENT_ID', '')
    SSO_CLIENT_SECRET = os.getenv('SSO_CLIENT_SECRET', '')
    SSO_REDIRECT_URI = os.getenv('WEB_URL','') + '/auth/login'

    UPLOAD_FOLDER = os.getenv('UPLOAD_FOLDER', 'static/logos')
